#include "system.h"
#include "coremain.h"

/*==========================================================================*/
/* Startup routine                                                          */
/*==========================================================================*/

/*==========================================================================*/
/* Prototypes                                                               */
/*==========================================================================*/
extern const T_APP_HDR KAppHdr;
//extern const T_APP_HDR apphdr;
//extern void *apphdr;
extern void *_start_of_app;
extern void *_end_of_flash;
extern void *_start_of_data;
extern void *_end_of_bss;


int main(unsigned long aFuncNo, unsigned long aParam2, unsigned long aParam3);
//int main (int argc, char *argv[]);
void SegmentInit (void);
//void PowerOff (void);

/*==========================================================================*/
/* Application header                                                       */
/*==========================================================================*/
const unsigned long KStartupHeader[12] __attribute__ ((section ("sect_header"))) =
{
  (unsigned long)&KAppHdr,          // app table ptr
  (unsigned long)&_start_of_app,    // crc check start at 0x50
  (unsigned long)&_end_of_flash,    // crc check end address
  (unsigned long)0,                 // crc will be overrided
  (unsigned long)&_start_of_data,   // SFB(DATA_I)
  (unsigned long)&_end_of_bss,      // SFE(DATA_N), Maybe 
	(unsigned long)(((unsigned long)MY_APPL_ID) | ((unsigned long)SOFT_VERSION<<8) | ((unsigned long)SOFT_SUBVERSION<<16) | ((unsigned long)APPL_TYPE<<24)),
//	(unsigned long)(((unsigned long)K_AppID) | ((unsigned long)K_Version<<8) | ((unsigned long)K_SubVersion<<16) | ((unsigned long)K_Type<<24)),
//	(unsigned long)(((unsigned long)apphdr.b_AppId) | ((unsigned long)apphdr.b_version<<8) | ((unsigned long)apphdr.b_subver<<16) | ((unsigned long)apphdr.b_type<<24)),
  (unsigned long)0,                 // will be filled with YYYYMMDD                
  (unsigned long)0,                 // will be filled with HHMMSSNN
  (unsigned long)0,                 // fill with TMS checksum, display checksum
  (unsigned long)0,                  // crc of YYYYMMDDHHMMSSNN, TMS checksum & display checksum, dummy
  (unsigned long)0
};

/*==========================================================================*/
/* Application entry point                                                  */
/*==========================================================================*/
int cstart_main (unsigned long aFuncNo, unsigned long aParam2, unsigned long aParam3)
{
  SegmentInit();
  main(aFuncNo, aParam2, aParam3);
  //os_appcall_return(0);
  //PowerOff ();
  return 0;
}

int cstartup (unsigned long aFuncNo, unsigned long aParam2, unsigned long aParam3) __attribute__ ((section ("sect_startup")));
int cstartup (unsigned long aFuncNo, unsigned long aParam2, unsigned long aParam3)
{
  return cstart_main(aFuncNo, aParam2, aParam3);
}

/* PC0711
int cstartup (int argc, char *argv[]) __attribute__ ((section ("sect_startup")));
int cstartup (int argc, char *argv[])
{
    SegmentInit ();
    main (argc, argv);
    PowerOff ();
    return 0;
}
*/

/*==========================================================================*/
/* Signature for Sign                                                       */
/*==========================================================================*/
#if (SIGN != 0)		//PC0802  no more use KKek!!
    const unsigned char KKek[8] __attribute__ ((section ("sect_kek"))) = "Spectra\x00";
#else
    const unsigned char KKek[8] = "Spectra\x00";
#endif
